package com.example.rdcproject.ving.notes.service;

import com.example.rdcproject.ving.notes.entity.User;

import java.sql.*;

public class TotalTime {

    private Connection connection;

    private PreparedStatement statement;

    User user = User.getInstance();

    public TotalTime() {
    }

    public void saveTotalStudyTime(Long totalStudyTime) {
        ConnectionPool connectionPool = ConnectionPool.getInstance();
        try{
            connection = connectionPool.getConnection();


            // 查询用户的当前学习时间
            String sql = "SELECT user_totalStudyTime FROM user WHERE user_nickname = '" + user.getNickname() + "'";
            statement = connection.prepareStatement(sql);

            ResultSet rs = statement.executeQuery();
            if (rs.next()) {
                // 将当前学习时间和总学习时间相加
                totalStudyTime +=rs.getLong("user_totalStudyTime");

            }
            // 更新用户的总学习时间
            String sql2 = "UPDATE user set user_totalStudyTime = "+totalStudyTime+" WHERE user_nickname = '" +user.getNickname() +"'";
            statement.executeUpdate(sql2);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }




}
